package com.max.back.demo.api;

import cn.hutool.core.bean.BeanUtil;
import com.max.back.common.http.ResponseResult;

import com.max.back.demo.entity.Doctor;

import com.max.back.demo.form.DoctorForm;
import com.max.back.demo.form.DrugTypeDeleteForm;
import com.max.back.demo.service.DoctorSer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * DoctorCtl
 */
@CrossOrigin
@RestController
@RequestMapping("/api/doctor")
public class DoctorCtl {
    @Autowired
    private DoctorSer ser;
    @PostMapping("/add")
    public ResponseResult<Boolean> add(@RequestBody @Validated({DoctorForm.Base.class}) DoctorForm form) {
        Doctor doctor = BeanUtil.toBean(form, Doctor.class);
        boolean res = ser.checkandinsert(doctor);
        if (res) {
            return ResponseResult.getSuccessResult(true, "A006", null);
        } else {
            return ResponseResult.getMessageResult(false, "A007");
        }
    }
    @PostMapping("/delete")
    public ResponseResult<Boolean> delete(@RequestBody @Validated({DoctorForm.Delete.class}) DoctorForm form) {
        boolean res = ser.removeById(form.getId());
        if (res) {
            return ResponseResult.getSuccessResult(true, "A006", null);
        } else {
            return ResponseResult.getMessageResult(false, "A007");
        }
    }
    @PostMapping("/countdelete")
    public ResponseResult<Boolean> countdelete(@RequestBody @Valid DrugTypeDeleteForm form) {
        boolean res = false;
        for (String id : form.getIds()) {
            res = ser.removeById(id);
        }
        if (res) {
            return ResponseResult.getSuccessResult(true, "A006", null);
        } else {
            return ResponseResult.getMessageResult(false, "A007");
        }
    }
    @PostMapping("/update")
    public ResponseResult<Boolean> update(@RequestBody @Validated({DoctorForm.Update.class}) DoctorForm form) {
        Doctor doctor = BeanUtil.toBean(form, Doctor.class);
        boolean res = ser.updateById(doctor);
        if (res) {
            return ResponseResult.getSuccessResult(true, "A006", null);
        } else {
            return ResponseResult.getMessageResult(false, "A007");
        }
    }
    @PostMapping("/find")
    public ResponseResult<List<Doctor>> find(@RequestBody @Validated({DoctorForm.Find.class}) DoctorForm form) {
        List<Doctor> list = ser.finddoctor(form.getId(), form.getSex(), form.getDept());
        return ResponseResult.getSuccessResult(list);
    }
}
